<template>
	<view class="page">
		<image src="../../static/register/top.jpg" mode="widthFix" class="top" />
		<view class="location-container">
			<view class="left">
				<image src="../../static/register/location.png" mode="widthFix" class="location"></image>
				<text>嘿代驾服务地点</text>
			</view>
			<view class="right">湖北省荆门市</view>
		</view>
		<view class="info-container">
			<view class="title-container">
				<image src="../../static/register/title-bg.png" mode="widthFix" class="title-bg"></image>
				<text class="title">基本要求</text>
			</view>
			<view class="list">
				<view class="item">
					<text>1.</text>
					三年安全驾驶经验；
				</view>
				<view class="item">
					<text>2.</text>
					年龄23~55周岁；
				</view>
				<view class="item">
					<text>3.</text>
					无违法犯罪记录、无精神病史、无吸毒史，以及平台认为不适合嘿代驾的其他历史证明；
				</view>
				<view class="item">
					<text>4.</text>
					有熟练驾车经验；
				</view>
				<view class="item">
					<text>5.</text>
					身体健康，无肢体残疾和大面积纹身；
				</view>
				<view class="item">
					<text>6.</text>
					需要提供身份证、驾驶证、直系亲属联系方式，并保存前述材料的真实合法性；
				</view>
			</view>
		</view>
		<view class="info-container">
			<view class="title-container">
				<image src="../../static/register/title-bg.png" mode="widthFix" class="title-bg"></image>
				<text class="title">基本要求</text>
			</view>
			<view class="list">
				<view class="complex-item">
					<view class="left">01</view>
					<view class="right">
						<text class="item-title">在线注册</text>
						<text class="item-desc">在小程序上完成注册</text>
					</view>
				</view>
				<view class="complex-item">
					<view class="left">02</view>
					<view class="right">
						<text class="item-title">基本信息提交</text>
						<text class="item-desc">按照流程提交本人身份证、驾驶证</text>
					</view>
				</view>
				<view class="complex-item">
					<view class="left">03</view>
					<view class="right">
						<text class="item-title">信息审核</text>
						<text class="item-desc">对提交信息以及证件进行审核</text>
					</view>
				</view>
				<view class="complex-item">
					<view class="left">04</view>
					<view class="right">
						<text class="item-title">签署合同</text>
						<text class="item-desc">收到邮寄的合同后必须本人签署</text>
					</view>
				</view>
			</view>
		</view>
		<!-- <button class="btn" open-type="getUserInfo" @tap="register()">立即注册</button> -->
		<!-- @tap="register()" -->
		<button class="btn" @getphonenumber="register" open-type="getPhoneNumber">立即注册</button>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
export default {
	data() {
		return {
			code: null
		};
	},
	methods: {
		async register(e) {
			
			let that = this;
			
			//解构出回调的code,此code是用于解析用户手机号的code
			let { code } = e.detail;
			// console.log('code是:', code);

			//获取手机号码
			let phoneNum = await this.getPhonNumber(code);
			console.log('用户手机号是:', phoneNum);

			//本地存储一个手机号 uni.getStorageSync("contact_tel")
			uni.setStorageSync('contact_tel', phoneNum);

			//获取微信登录的code
			let wxLoginCode = await this.getWxLoginCode();
			// console.log('获取到的登录code是:',wxLoginCode)

			//获取用户信息
			let { nickname, avatarUrl } = await this.getUserInfo();
			// console.log(`获取到的用户信息是:${nickname}----${avatarUrl}`);

			//请求注册接口
			this.registerAction({
				code: wxLoginCode,
				nickname: nickname,
				photo: avatarUrl,
				tel: phoneNum
			});
		},

		//获取微信登录信息
		getWxLoginCode() {
			return new Promise((resolve, reject) => {
				uni.login({
					success(resp) {
						let code = resp.code;
						this.code = code;
						resolve(code);
					}
				});
			});
		},

		//获取登录的用户信息
		getUserInfo() {
			return new Promise((resolve, reject) => {
				uni.getUserInfo({
					provider: 'weixin',
					success: resp => {
						let nickname = resp.userInfo.nickName;
						let avatarUrl = resp.userInfo.avatarUrl;
						resolve({ nickname, avatarUrl });
					}
				});
			});
		},

		//获取微信凭证
		// getAccessToken() {
		// 	return new Promise((resolve, reject) => {
		// 		console.log(`APP_ID的值是:${this.APP_ID}------${this.APP_SECRET}`);
		// 		uni.request({
		// 			url: `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${this.APP_ID}&secret=${this.APP_SECRET}`,
		// 			success: res => {
		// 				let { access_token } = res.data;
		// 				resolve(access_token);
		// 			}
		// 		});
		// 	});
		// },

		//解析电话号码
		// parsePhoneNumber(access_token, code) {
		// 	return new Promise((resolve, reject) => {
		// 		uni.request({
		// 			method: 'POST',
		// 			url: `https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=${access_token}`,
		// 			data: {
		// 				code: code
		// 			},
		// 			success: res => {
		// 				resolve(res.data.phone_info.phoneNumber);
		// 			},
		// 			fail: err => {
		// 				console.log(`错误码是:`, err);
		// 			}
		// 		});
		// 	});
		// },

		//获取手机号
		getPhonNumber(code) {
			return new Promise((resolve, reject) => {
				this.ajax(this.url.parsePhoneNumber, 'GET', { photeCode: code }, resp => {
					// console.log('请求结果是:', resp);
					resolve(resp.data.msg);
				});
			});
		},

		//请求注册接口
		registerAction(data) {
			// console.log('执行注册接口了',data)
			this.ajax(this.url.registerNewDriver, 'POST', data, resp => {
				// console.log(resp);
				//这里通过服务器返回的注册信息来判定跳转结果
				let token = resp.data.token;
				uni.setStorageSync('token', token);
				uni.setStorageSync('realAuth', 1);
				this.$refs.uToast.show({
					title: '注册成功',
					type: 'success',
					callback: function() {
						uni.redirectTo({
							url: '../../identity/filling/filling?mode=create'
						});
					}
				});
			});
		}
	}
};
</script>

<style lang="less">
@import url('register.less');
</style>
